package com.orangelife.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.orangelife.exception.AppBizException;

/***
 * 橙子生活 json 转map
 * 
 * @author shendiao
 * 
 */
public class JsonMapUtils {

	/***
	 * 将返回里面的entity 转为map
	 * 
	 * @param jsonObject
	 * @return
	 * @throws AppBizException
	 */
	public static Map<String, Object> getEntityMap(JSONObject jsonObject)
			throws AppBizException {
		if (jsonObject == null) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (!jsonObject.containsKey("errCode")) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		if (!String.valueOf(jsonObject.get("errCode")).equals("0")) {
			throw new AppBizException(Integer.parseInt(jsonObject
					.get("errCode").toString()), jsonObject.get("errInfo")
					.toString());
		}
		map.put("errCode", jsonObject.getInteger("errCode"));
		map.put("errInfo", jsonObject.getString("errInfo"));
		if (jsonObject.containsKey("entity")) {
			if (jsonObject.get("entity") != null) {
				String entity = jsonObject.get("entity").toString();
				net.sf.json.JSONObject jsEntity = net.sf.json.JSONObject
						.fromObject(entity);
				Iterator<?> keys = jsEntity.keys();
				while (keys.hasNext()) {
					String key = (String) keys.next();
					map.put(key, jsEntity.get(key));
				}
			}
		}
		return map;
	}

	/***
	 * 将返回里面的pageEntity 转为 List<Map<String, Object>>
	 * 
	 * @param jsonObject
	 * @return
	 * @throws AppBizException
	 */
	public static List<Map<String, Object>> getPageEntityMap(
			JSONObject jsonObject) throws AppBizException {
		if (jsonObject == null) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (!jsonObject.containsKey("errCode")) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		if (!String.valueOf(jsonObject.get("errCode")).equals("0")) {
			throw new AppBizException(Integer.parseInt(jsonObject
					.get("errCode").toString()), jsonObject.get("errInfo")
					.toString());
		}
		if (jsonObject.containsKey("pageEntity")) {
			if (jsonObject.get("pageEntity") != null) {
				String entity = jsonObject.get("pageEntity").toString();
				net.sf.json.JSONArray jsPageEntity = net.sf.json.JSONArray
						.fromObject(entity);
				for (int i = 0; i < jsPageEntity.size(); i++) {
					Iterator<?> keys = jsPageEntity.getJSONObject(i).keys();
					Map<String, Object> map = new HashMap<String, Object>();
					while (keys.hasNext()) {

						String key = (String) keys.next();
						Object value = jsPageEntity.getJSONObject(i).get(key);

						map.put(key, value);

					}
					list.add(map);
				}
			}
		}
		return list;
	}

	public static List<String> getPageEntityList(JSONObject jsonObject)
			throws AppBizException {
		if (jsonObject == null) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		List<String> list = new ArrayList<String>();
		if (!jsonObject.containsKey("errCode")) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		if (!String.valueOf(jsonObject.get("errCode")).equals("0")) {
			throw new AppBizException(Integer.parseInt(jsonObject
					.get("errCode").toString()), jsonObject.get("errInfo")
					.toString());
		}
		if (jsonObject.containsKey("pageEntity")) {
			if (jsonObject.get("pageEntity") != null) {
				String entity = jsonObject.get("pageEntity").toString();
				net.sf.json.JSONArray jsPageEntity = net.sf.json.JSONArray
						.fromObject(entity);
				for (int i = 0; i < jsPageEntity.size(); i++) {
					list.add(jsPageEntity.getString(i));
				}
			}
		}

		return list;
	}

	public static Map<String, Object> getEntityMaps(JSONObject jsonObject)
			throws AppBizException {
		if (jsonObject == null) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (!jsonObject.containsKey("errCode")) {
			throw new AppBizException(30001, "亲，请求失败，请重试");
		}
		if (!String.valueOf(jsonObject.get("errCode")).equals("0")) {
			throw new AppBizException(Integer.parseInt(jsonObject
					.get("errCode").toString()), jsonObject.get("errInfo")
					.toString());
		}
		map.put("errCode", jsonObject.getInteger("errCode"));
		map.put("errInfo", jsonObject.getString("errInfo"));
		if (jsonObject.containsKey("entity")) {
			if (jsonObject.get("entity") != null) {
				String entity = jsonObject.get("entity").toString();
				net.sf.json.JSONObject jsEntity = net.sf.json.JSONObject
						.fromObject(entity);
				Iterator<?> keys = jsEntity.keys();
				while (keys.hasNext()) {
					String key = (String) keys.next();
					map.put(key, jsEntity.get(key));
				}
			}
		}
		return map;
	}
}
